/**
 * Created by zeopean on 2015/10/9.
 */
$(function(){
    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    /** 项目里程碑 */
    var Updated = 0;
    $('.save_mil').bind('click' , function(){
        var pro_id  = $('#project_id').val();
        var mil_id  = $('input[name=mil_id]').val();
        if(pro_id == 0 ){
            noty({text:'您的项目名不能为空',type:'error',timeout:1000});
            return false;
        }
        $('#form_mil input[name=project_id]').val(pro_id);
        var url = EditProjectMilestone + mil_id ;

        $.ajax({
            type:"post",
            url:url,
            dataType:"json",
            data:$('#form_mil').serialize(),
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,timeout:500});

                    /** 请求数据 */
                    $.ajax({
                        type: "get",
                        url: getMilestoneByProId,
                        dataType: "json",
                        data: {
                            'proId': pro_id
                        },
                        success: function (obj) {
                            if (obj.level === "success") {
                                var Str = '';
                                for(var i in obj.data){
                                    var tmpe = obj.data[i].mil_time ;
                                    Str  += "<span><label>"+ tmpe.substring(0,10)+"<br/><input class='input-xlarge' value='"+obj.data[i].mil_desc+"' />&nbsp;<button type='button' data='"+obj.data[i].id+"' class='btn del_mil'>删除</button><button type='button' data='"+obj.data[i].id+"' class='btn update_mil'>编辑</button></span>";
                                }

                                $('.milestone').html(Str);
                            }
                        }
                    });

                }else{
                    var datajson = obj.responseJSON;
                    noty({text:datajson.message,type:datajson.level,timeout:1000});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });
    });

    /** 删除里程碑 */
    $(document).on('click' , ".del_mil",  function(){
        var mil_id  = $(this).attr('data');
        var milestone = $(this);
        $.ajax({
            type:"get",
            url:delMilestone,
            dataType:"json",
            data:{
                'id'  :   mil_id
            },
            success:function(obj){
                if(obj.level === "success"){
                    milestone.parent().hide();
                }else{
                    noty({text:obj.message,type:obj.level,timeout:1000});
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:1000});
            }
        });

    });

    /**
     * 编辑操作
     */
    $(document).on('click' , '.update_mil' , function(){
        var mil_id  = $(this).attr('data');
        var milestone = $(this);
        var mil_time = milestone.prevAll('label').html();
        var mil_desc = milestone.prevAll('input').val();

        $('input[name=mil_time]').val(mil_time);
        $('input[name=mil_desc]').val(mil_desc);
        $('input[name=mil_id]').val(mil_id);
        Updated = 1;

    });

    /** end */
});
